package cn.zhonghuanzhiyuan.sprc.common.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

@Slf4j
public class FileUtil {

    public static void upload(String baseDir, String fileName, InputStream inputStream) throws IOException {
        Path path = Paths.get(composeFilePath(baseDir, fileName));
        try {
            Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            log.error("Upload Exception", e.getCause());
            throw e;
        }
    }
    public static String composeFilePath(final String baseDir, final String fileName){
        String dir = baseDir;
        dir = !dir.endsWith(File.separator) ? dir + File.separator : dir;
        return dir + fileName;
    }
}
